package com.douqu.game.core.config;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-25 12:08
 */
public class ProfessionConfig extends GameObject {

    /**
     * 克制类型(其它职业)
     */
    public int restrictType;

    /**
     * 对各种职业的伤害加成(万分比)
     *
     */
    public CommonData[] damagePlus;

    /**
     * 对各种职业的免伤加成(万分比)
     */
    public CommonData[] exdamagePlus;

    @Override
    public boolean check() {

        if(damagePlus == null)
        {
            LogUtils.error("Load Profession Error -> damagePlus is null,id = " + id);
            return false;
        }
        if(exdamagePlus == null)
        {
            LogUtils.error("Load Profession Error -> exdamagePlus is null,id = " + id);
            return false;
        }

        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROFESSION_KEY, restrictType) == null)
        {
            LogUtils.error("Load Profession Error -> restrictType is null,id = " + id);
            return false;
        }

        for(CommonData commonData : damagePlus)
        {
            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROFESSION_KEY, commonData.id) == null)
            {
                LogUtils.error("Load Profession Error -> damagePlus is null,id = " + id + " commonData.id:" + commonData.id);
                return false;
            }
        }

        for(CommonData commonData : exdamagePlus)
        {
            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROFESSION_KEY, commonData.id) == null)
            {
                LogUtils.error("Load Profession Error -> exdamagePlus is null,id = " + id + " commonData.id:" + commonData.id);
                return false;
            }
        }

        return true;
    }


    /**
     * 获取伤害加成
     * @param profession
     * @return
     */
    public int getDamagePlus(int profession)
    {
        for(CommonData commonData : damagePlus)
        {
            if(commonData.id == profession)
                return commonData.value;
        }

        return 0;
    }

    /**
     * 获取免伤加成
     * @param profession
     * @return
     */
    public int getExdamagePlus(int profession)
    {
        for(CommonData commonData : exdamagePlus)
        {
            if(commonData.id == profession)
                return commonData.value;
        }

        return 0;
    }




    @Override
    public boolean setVariable(String key, String value)
    {
       if("damagePlus".equals(key))
       {
           damagePlus = LoadUtils.loadCommonDataToArray(key, value);
           return true;
       }
       else if("exdamagePlus".equals(key))
       {
           exdamagePlus = LoadUtils.loadCommonDataToArray(key, value);
           return true;
       }
       else
           return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "Profession{" +
                "damagePlus=" + Arrays.toString(damagePlus) +
                ", exdamagePlus=" + Arrays.toString(exdamagePlus) +
                "} " + super.toString();
    }
}
